#!/bin/sh /etc/rc.common
# Copyright (C) 2018-2019 Lienol

START=99

CONFIG="kodexplorer"

TEMP_PATH="/var/etc/kodexplorer"
NGINX_CONFIG="$TEMP_PATH/nginx.conf"
PHP_FPM_CONFIG="$TEMP_PATH/php-fpm.conf"
PHP_CONFIG="/etc/php.ini"
PHP_BACKUP_CONFIG="/etc/php.ini.backup"

config_t_get() {
	local index=0
	[ -n "$4" ] && index=$4
	local ret=$(uci get $CONFIG.@$1[$index].$2 2>/dev/null)
	echo ${ret:=$3}
}

gen_nginx_config() {
	port=$(config_t_get global port)
	project_directory=$(config_t_get global project_directory)
	cat <<-EOF >$NGINX_CONFIG
user root root;
worker_processes  1;
pid        /var/run/nginx_kodexplorer.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       $port;
        server_name  localhost;
        location / {
            root $project_directory;
            index index.html index.htm index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root $project_directory;
			try_files \$uri = 404; # PHP 文件不存在返回404
            fastcgi_pass unix:/var/run/php7-fpm.sock; # 通过 Unix 套接字执行 PHP
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; # 修复 Nginx fastcgi 漏洞
            include /etc/nginx/fastcgi_params;
        }
    }
}
	EOF
}

gen_php_config() {
	storage_device_path=$(config_t_get global storage_device_path)
	memory_limit=$(config_t_get global memory_limit)
	post_max_size=$(config_t_get global post_max_size)
	upload_max_filesize=$(config_t_get global upload_max_filesize)
	cp $PHP_CONFIG $PHP_BACKUP_CONFIG
	cat <<-EOF >$PHP_CONFIG
[PHP]
zend.ze1_compatibility_mode = Off
engine = On
precision    =  12
y2k_compliance = On
output_buffering = Off
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 100

open_basedir = $storage_device_path:/tmp/:/proc/
disable_functions =
disable_classes =
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = $memory_limit
error_reporting  =  E_ALL & ~E_NOTICE & ~E_STRICT

display_errors = On
display_startup_errors = Off
log_errors = Off
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off

variables_order = "EGPCS"
request_order = "GP"
register_globals = Off
register_long_arrays = Off
register_argc_argv = On
auto_globals_jit = On
post_max_size = $post_max_size
magic_quotes_runtime = Off
magic_quotes_sybase = Off
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"

;doc_root = "/www"
user_dir =
extension_dir = "/usr/lib/php"
enable_dl = On
cgi.fix_pathinfo=1

file_uploads = On
upload_tmp_dir = "/tmp"
upload_max_filesize = $upload_max_filesize
max_file_uploads = 20

allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
EOF
	cat <<-EOF >$PHP_FPM_CONFIG
[global]
pid = /var/run/kodexplorer_php7-fpm.pid
error_log = /var/log/kodexplorer_php7-fpm.log
[www]
user = root
listen = /var/run/php7-fpm.sock
listen.mode = 0666
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
EOF
}

start() {
	ENABLED=$(config_t_get global enable 0)
	[ "$ENABLED" = "0" ] && return 0
	mkdir -p $TEMP_PATH /var/log/nginx /var/lib/nginx
	gen_nginx_config
	gen_php_config
	/usr/bin/php-fpm -R -y $PHP_FPM_CONFIG -g "/var/run/php7-fpm.pid"
	/usr/sbin/nginx -c $NGINX_CONFIG
}

stop() {
	kill -9 `cat /var/run/nginx_kodexplorer.pid` >/dev/null 2>&1 &
	killall -9 php-fpm nginx >/dev/null 2>&1 &
	rm -f /var/run/nginx_kodexplorer.pid
	rm -f /var/run/kodexplorer_php7-fpm.pid
	rm -f /var/log/kodexplorer_php7-fpm.log
	rm -f /var/run/php7-fpm.sock
	[ -f "$PHP_BACKUP_CONFIG" -a -f "$PHP_CONFIG" ] && {
		rm -f $PHP_CONFIG
		cp $PHP_BACKUP_CONFIG $PHP_CONFIG
		rm -f $PHP_BACKUP_CONFIG
	}
}

restart() {
	stop
	sleep 1
	start
}